{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "01eb12b0",
   "metadata": {},
   "source": [
    "# Python程序设计与数据处理 Class 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bc4756e",
   "metadata": {},
   "source": [
    "# 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ade45bd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入pandas库和numpy库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "#设置pandas显示所有列\n",
    "pd.set_option('display.max_columns', None)\n",
    "# pd.set_option('display.max_rows',None)是用于显示所有行的设置（当前未启用）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84ac4a93",
   "metadata": {},
   "source": [
    "# 下载数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "185c21a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Encoding: ascii, Confidence: 1.0\n"
     ]
    }
   ],
   "source": [
    "import chardet\n",
    "\n",
    "def check_encoding(filename):\n",
    "    #check_encoding函数通过读取文件二进制数据\n",
    "    rawdata = open(filename, 'rb').read()\n",
    "    result = chardet.detect(rawdata)\n",
    "    encoding = result['encoding']\n",
    "    confidence = result['confidence']\n",
    "    #调用chardet.detect方法检测编码（encoding）和置信度（confidence，表示检测结果的可靠程度）\n",
    "    return encoding, confidence\n",
    "\n",
    "file_path = 'D:/笃行楼209/000001.csv'# 添加文件路径\n",
    "encoding, confidence = check_encoding(file_path)\n",
    "print(f\"Encoding: {encoding}, Confidence: {confidence}\")\n",
    "#，置信度1.0（即检测结果完全可靠）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "43355c9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Day</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990/12/19</td>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1990/12/20</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1990/12/21</td>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1990/12/24</td>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1990/12/25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8468</th>\n",
       "      <td>2025/8/25</td>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8469</th>\n",
       "      <td>2025/8/26</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8470</th>\n",
       "      <td>2025/8/27</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8471</th>\n",
       "      <td>2025/8/28</td>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8472</th>\n",
       "      <td>2025/8/29</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Day  Preclose      Open   Highest    Lowest     Close\n",
       "0     1990/12/19              96.050    99.980    95.790    99.980\n",
       "1     1990/12/20     99.98   104.300   104.390    99.980   104.390\n",
       "2     1990/12/21    104.39   109.070   109.130   103.730   109.130\n",
       "3     1990/12/24    109.13   113.570   114.550   109.130   114.550\n",
       "4     1990/12/25    114.55   120.090   120.250   114.550   120.250\n",
       "...          ...       ...       ...       ...       ...       ...\n",
       "8468   2025/8/25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "8469   2025/8/26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "8470   2025/8/27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "8471   2025/8/28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "8472   2025/8/29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 6 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入数据\n",
    "data = pd.read_csv('D:/笃行楼209/000001.csv') # 导入CSV文件#\n",
    "data # 输出导入的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b2a12ee6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell  # 导入Jupyter交互式Shell的核心模块\n",
    "\n",
    "# 设置Jupyter Notebook的输出模式为'all'，这样每个单元格中所有语句的结果都会被依次输出（默认只输出最后一个表达式的结果）\n",
    "InteractiveShell.ast_node_interactivity = 'all'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b208bd95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Day</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990/12/19</td>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1990/12/20</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1990/12/21</td>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1990/12/24</td>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1990/12/25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8468</th>\n",
       "      <td>2025/8/25</td>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8469</th>\n",
       "      <td>2025/8/26</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8470</th>\n",
       "      <td>2025/8/27</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8471</th>\n",
       "      <td>2025/8/28</td>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8472</th>\n",
       "      <td>2025/8/29</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Day  Preclose      Open   Highest    Lowest     Close\n",
       "0     1990/12/19              96.050    99.980    95.790    99.980\n",
       "1     1990/12/20     99.98   104.300   104.390    99.980   104.390\n",
       "2     1990/12/21    104.39   109.070   109.130   103.730   109.130\n",
       "3     1990/12/24    109.13   113.570   114.550   109.130   114.550\n",
       "4     1990/12/25    114.55   120.090   120.250   114.550   120.250\n",
       "...          ...       ...       ...       ...       ...       ...\n",
       "8468   2025/8/25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "8469   2025/8/26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "8470   2025/8/27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "8471   2025/8/28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "8472   2025/8/29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 6 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Day  Preclose      Open   Highest    Lowest     Close\n",
      "0     1990/12/19              96.050    99.980    95.790    99.980\n",
      "1     1990/12/20     99.98   104.300   104.390    99.980   104.390\n",
      "2     1990/12/21    104.39   109.070   109.130   103.730   109.130\n",
      "3     1990/12/24    109.13   113.570   114.550   109.130   114.550\n",
      "4     1990/12/25    114.55   120.090   120.250   114.550   120.250\n",
      "...          ...       ...       ...       ...       ...       ...\n",
      "8468   2025/8/25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
      "8469   2025/8/26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
      "8470   2025/8/27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
      "8471   2025/8/28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
      "8472   2025/8/29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
      "\n",
      "[8473 rows x 6 columns]\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_csv('D:/笃行楼209/000001.csv') # 导入CSV文件\n",
    "data\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2631333c",
   "metadata": {},
   "source": [
    "# 了解变量的格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f0e6fa86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data)\n",
    "#查看变量data的类型，输出结果说明data是一个pandas的DataFrame对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aeba0406",
   "metadata": {},
   "source": [
    "# 打印数据框的列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "97f4a567",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['Day', 'Preclose', 'Open', 'Highest', 'Lowest', 'Close'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "print(data.columns)\n",
    "#输出数据框的列名对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1e1d0418",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Day' 'Preclose' 'Open' 'Highest' 'Lowest' 'Close']\n"
     ]
    }
   ],
   "source": [
    "print(data.columns.values)\n",
    "#输出列名的numpy数组形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f5906ba4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8473 entries, 0 to 8472\n",
      "Data columns (total 6 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   Day       8473 non-null   object \n",
      " 1   Preclose  8473 non-null   object \n",
      " 2   Open      8473 non-null   float64\n",
      " 3   Highest   8473 non-null   float64\n",
      " 4   Lowest    8473 non-null   float64\n",
      " 5   Close     8473 non-null   float64\n",
      "dtypes: float64(4), object(2)\n",
      "memory usage: 397.3+ KB\n"
     ]
    }
   ],
   "source": [
    "data.info()\n",
    "#查看各列的数据类型、非空值数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a00b4c87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Day</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990/12/19</td>\n",
       "      <td></td>\n",
       "      <td>96.05</td>\n",
       "      <td>99.98</td>\n",
       "      <td>95.79</td>\n",
       "      <td>99.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1990/12/20</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.30</td>\n",
       "      <td>104.39</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1990/12/21</td>\n",
       "      <td>104.39</td>\n",
       "      <td>109.07</td>\n",
       "      <td>109.13</td>\n",
       "      <td>103.73</td>\n",
       "      <td>109.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1990/12/24</td>\n",
       "      <td>109.13</td>\n",
       "      <td>113.57</td>\n",
       "      <td>114.55</td>\n",
       "      <td>109.13</td>\n",
       "      <td>114.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1990/12/25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.09</td>\n",
       "      <td>120.25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Day  Preclose    Open  Highest  Lowest   Close\n",
       "0  1990/12/19             96.05    99.98   95.79   99.98\n",
       "1  1990/12/20     99.98  104.30   104.39   99.98  104.39\n",
       "2  1990/12/21    104.39  109.07   109.13  103.73  109.13\n",
       "3  1990/12/24    109.13  113.57   114.55  109.13  114.55\n",
       "4  1990/12/25    114.55  120.09   120.25  114.55  120.25"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()\n",
    "#查看数据前几行内容，结合列名理解数据含义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2e3c9d4",
   "metadata": {},
   "source": [
    "# 时间序列数据的格式 Time-Series Data 以及 数据框 Dataframe的基本知识"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eee9660a",
   "metadata": {},
   "source": [
    "# 选择某一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "45d435fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         99.980\n",
       "1        104.390\n",
       "2        109.130\n",
       "3        114.550\n",
       "4        120.250\n",
       "          ...   \n",
       "8468    3883.562\n",
       "8469    3868.382\n",
       "8470    3800.350\n",
       "8471    3843.597\n",
       "8472    3857.927\n",
       "Name: Close, Length: 8473, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['Close']\n",
    "#选取数据框data中名为Close的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c39223e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         99.980\n",
       "1        104.390\n",
       "2        109.130\n",
       "3        114.550\n",
       "4        120.250\n",
       "          ...   \n",
       "8468    3883.562\n",
       "8469    3868.382\n",
       "8470    3800.350\n",
       "8471    3843.597\n",
       "8472    3857.927\n",
       "Name: Close, Length: 8473, dtype: float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.Close\n",
    "#与data['Close']效果一致，是选取列的另一种语法（当列名无特殊字符时可用）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "16dab1cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8468</th>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8469</th>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8470</th>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8471</th>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8472</th>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Close\n",
       "0       99.980\n",
       "1      104.390\n",
       "2      109.130\n",
       "3      114.550\n",
       "4      120.250\n",
       "...        ...\n",
       "8468  3883.562\n",
       "8469  3868.382\n",
       "8470  3800.350\n",
       "8471  3843.597\n",
       "8472  3857.927\n",
       "\n",
       "[8473 rows x 1 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[['Close']]\n",
    "#选取Close列并以DataFrame类型返回（因为传入的是列名列表）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ba9d1bbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data['Close'])\n",
    "#查看data['Close']的类型，输出为pandas.core.series.Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0520a858",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data[['Close']])\n",
    "#查看data[['Close']]的类型，输出为pandas.core.frame.DataFrame "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0b6e4a79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8468</th>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8469</th>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8470</th>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8471</th>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8472</th>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Close\n",
       "0       99.980\n",
       "1      104.390\n",
       "2      109.130\n",
       "3      114.550\n",
       "4      120.250\n",
       "...        ...\n",
       "8468  3883.562\n",
       "8469  3868.382\n",
       "8470  3800.350\n",
       "8471  3843.597\n",
       "8472  3857.927\n",
       "\n",
       "[8473 rows x 1 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[['Close']]\n",
    "#选取Close列并以DataFrame类型返回，可用于需要数据框格式的后续操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47326f8f",
   "metadata": {},
   "source": [
    "# 如何选择多列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d41b9619",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td></td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>109.13</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8468</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8469</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8470</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8471</th>\n",
       "      <td>3800.35</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8472</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Preclose     Close\n",
       "0                 99.980\n",
       "1        99.98   104.390\n",
       "2       104.39   109.130\n",
       "3       109.13   114.550\n",
       "4       114.55   120.250\n",
       "...        ...       ...\n",
       "8468  3825.759  3883.562\n",
       "8469  3883.562  3868.382\n",
       "8470  3868.382  3800.350\n",
       "8471   3800.35  3843.597\n",
       "8472  3843.597  3857.927\n",
       "\n",
       "[8473 rows x 2 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[['Preclose','Close']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc7e1251",
   "metadata": {},
   "source": [
    "# 如何选择行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a315710b",
   "metadata": {},
   "source": [
    "# 按照行数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b070db6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Day</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990/12/19</td>\n",
       "      <td></td>\n",
       "      <td>96.05</td>\n",
       "      <td>99.98</td>\n",
       "      <td>95.79</td>\n",
       "      <td>99.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1990/12/20</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.30</td>\n",
       "      <td>104.39</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1990/12/21</td>\n",
       "      <td>104.39</td>\n",
       "      <td>109.07</td>\n",
       "      <td>109.13</td>\n",
       "      <td>103.73</td>\n",
       "      <td>109.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1990/12/24</td>\n",
       "      <td>109.13</td>\n",
       "      <td>113.57</td>\n",
       "      <td>114.55</td>\n",
       "      <td>109.13</td>\n",
       "      <td>114.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1990/12/25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.09</td>\n",
       "      <td>120.25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1990/12/26</td>\n",
       "      <td>120.25</td>\n",
       "      <td>125.27</td>\n",
       "      <td>125.27</td>\n",
       "      <td>120.25</td>\n",
       "      <td>125.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1990/12/27</td>\n",
       "      <td>125.27</td>\n",
       "      <td>125.27</td>\n",
       "      <td>125.28</td>\n",
       "      <td>125.27</td>\n",
       "      <td>125.28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Day  Preclose    Open  Highest  Lowest   Close\n",
       "0  1990/12/19             96.05    99.98   95.79   99.98\n",
       "1  1990/12/20     99.98  104.30   104.39   99.98  104.39\n",
       "2  1990/12/21    104.39  109.07   109.13  103.73  109.13\n",
       "3  1990/12/24    109.13  113.57   114.55  109.13  114.55\n",
       "4  1990/12/25    114.55  120.09   120.25  114.55  120.25\n",
       "5  1990/12/26    120.25  125.27   125.27  120.25  125.27\n",
       "6  1990/12/27    125.27  125.27   125.28  125.27  125.28"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 该代码用于选择data数据框中的第0行到第6行（共7行），即通过切片操作选取前7行数据。\n",
    "data[0:7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "cff238bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Day</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990/12/19</td>\n",
       "      <td></td>\n",
       "      <td>96.05</td>\n",
       "      <td>99.98</td>\n",
       "      <td>95.79</td>\n",
       "      <td>99.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1990/12/20</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.30</td>\n",
       "      <td>104.39</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1990/12/21</td>\n",
       "      <td>104.39</td>\n",
       "      <td>109.07</td>\n",
       "      <td>109.13</td>\n",
       "      <td>103.73</td>\n",
       "      <td>109.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1990/12/24</td>\n",
       "      <td>109.13</td>\n",
       "      <td>113.57</td>\n",
       "      <td>114.55</td>\n",
       "      <td>109.13</td>\n",
       "      <td>114.55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Day  Preclose    Open  Highest  Lowest   Close\n",
       "0  1990/12/19             96.05    99.98   95.79   99.98\n",
       "1  1990/12/20     99.98  104.30   104.39   99.98  104.39\n",
       "2  1990/12/21    104.39  109.07   109.13  103.73  109.13\n",
       "3  1990/12/24    109.13  113.57   114.55  109.13  114.55"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 该代码使用iloc按照行号和列号进行数据选择。\n",
    "# data.iloc[0:4, 0:6]表示选取data数据框的第0行到第3行（共4行），以及第0列到第5列（共6列）的数据。\n",
    "data.iloc[0:4,0:6] #按行列号访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "19fe172f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120.09"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 该代码通过 data.at[4, 'Open'] 实现了对 DataFrame 中第 4行（索引为4）且列名为 'Open' 的单元格的访问，返回该位置的具体数值。at 适用于通过“行标签+列标签”快速定位单个元素，效率较高。\n",
    "data.at[4,'Open'] # 按行索引，列名访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "43712caf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "87    116.19\n",
       "Name: Highest, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用条件筛选来获取特定日期的开盘价\n",
    "# data[data['Day'] == \"1991/4/24\"] 先筛选出日期为\"1991/4/24\"的行\n",
    "# .Highest然后从筛选结果中提取'Highest'列（开盘价）\n",
    "data[data['Day'] == \"1991/4/24\"].Highest"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e817c5b7",
   "metadata": {},
   "source": [
    "# 按照时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2acf26af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Day</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990-12-19</td>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1990-12-20</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1990-12-21</td>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1990-12-24</td>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1990-12-25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8468</th>\n",
       "      <td>2025-08-25</td>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8469</th>\n",
       "      <td>2025-08-26</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8470</th>\n",
       "      <td>2025-08-27</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8471</th>\n",
       "      <td>2025-08-28</td>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8472</th>\n",
       "      <td>2025-08-29</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Day  Preclose      Open   Highest    Lowest     Close\n",
       "0    1990-12-19              96.050    99.980    95.790    99.980\n",
       "1    1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "2    1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "3    1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "4    1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...         ...       ...       ...       ...       ...       ...\n",
       "8468 2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "8469 2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "8470 2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "8471 2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "8472 2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 6 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将'Day'列从字符串格式转换为日期时间格式\n",
    "# pd.to_datetime() 函数用于将字符串转换为datetime对象\n",
    "# format='%Y/%m/%d' 指定了输入日期的格式：年/月/日（如1990/12/19）\n",
    "data['Day'] = pd.to_datetime(data['Day'],format = '%Y/%m/%d')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "50e938cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Day</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8472</th>\n",
       "      <td>2025-08-29</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8471</th>\n",
       "      <td>2025-08-28</td>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8470</th>\n",
       "      <td>2025-08-27</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8469</th>\n",
       "      <td>2025-08-26</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8468</th>\n",
       "      <td>2025-08-25</td>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1990-12-25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1990-12-24</td>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1990-12-21</td>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1990-12-20</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990-12-19</td>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Day  Preclose      Open   Highest    Lowest     Close\n",
       "8472 2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "8471 2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "8470 2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "8469 2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "8468 2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "...         ...       ...       ...       ...       ...       ...\n",
       "4    1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "3    1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "2    1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "1    1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "0    1990-12-19              96.050    99.980    95.790    99.980\n",
       "\n",
       "[8473 rows x 6 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照'Day'这一列对数据进行降序排序（即日期从新到旧）\n",
    "data = data.sort_values(by=['Day'], axis=0, ascending=False)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "31c2127a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method sort_values in module pandas.core.frame:\n",
      "\n",
      "sort_values(by: 'IndexLabel', *, axis: 'Axis' = 0, ascending: 'bool | list[bool] | tuple[bool, ...]' = True, inplace: 'bool' = False, kind: 'str' = 'quicksort', na_position: 'str' = 'last', ignore_index: 'bool' = False, key: 'ValueKeyFunc' = None) -> 'DataFrame | None' method of pandas.core.frame.DataFrame instance\n",
      "    Sort by the values along either axis.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "            by : str or list of str\n",
      "                Name or list of names to sort by.\n",
      "    \n",
      "                - if `axis` is 0 or `'index'` then `by` may contain index\n",
      "                  levels and/or column labels.\n",
      "                - if `axis` is 1 or `'columns'` then `by` may contain column\n",
      "                  levels and/or index labels.\n",
      "    axis : {0 or 'index', 1 or 'columns'}, default 0\n",
      "         Axis to be sorted.\n",
      "    ascending : bool or list of bool, default True\n",
      "         Sort ascending vs. descending. Specify list for multiple sort\n",
      "         orders.  If this is a list of bools, must match the length of\n",
      "         the by.\n",
      "    inplace : bool, default False\n",
      "         If True, perform operation in-place.\n",
      "    kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'\n",
      "         Choice of sorting algorithm. See also :func:`numpy.sort` for more\n",
      "         information. `mergesort` and `stable` are the only stable algorithms. For\n",
      "         DataFrames, this option is only applied when sorting on a single\n",
      "         column or label.\n",
      "    na_position : {'first', 'last'}, default 'last'\n",
      "         Puts NaNs at the beginning if `first`; `last` puts NaNs at the\n",
      "         end.\n",
      "    ignore_index : bool, default False\n",
      "         If True, the resulting axis will be labeled 0, 1, …, n - 1.\n",
      "    \n",
      "         .. versionadded:: 1.0.0\n",
      "    \n",
      "    key : callable, optional\n",
      "        Apply the key function to the values\n",
      "        before sorting. This is similar to the `key` argument in the\n",
      "        builtin :meth:`sorted` function, with the notable difference that\n",
      "        this `key` function should be *vectorized*. It should expect a\n",
      "        ``Series`` and return a Series with the same shape as the input.\n",
      "        It will be applied to each column in `by` independently.\n",
      "    \n",
      "        .. versionadded:: 1.1.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    DataFrame or None\n",
      "        DataFrame with sorted values or None if ``inplace=True``.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    DataFrame.sort_index : Sort a DataFrame by the index.\n",
      "    Series.sort_values : Similar method for a Series.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> df = pd.DataFrame({\n",
      "    ...     'col1': ['A', 'A', 'B', np.nan, 'D', 'C'],\n",
      "    ...     'col2': [2, 1, 9, 8, 7, 4],\n",
      "    ...     'col3': [0, 1, 9, 4, 2, 3],\n",
      "    ...     'col4': ['a', 'B', 'c', 'D', 'e', 'F']\n",
      "    ... })\n",
      "    >>> df\n",
      "      col1  col2  col3 col4\n",
      "    0    A     2     0    a\n",
      "    1    A     1     1    B\n",
      "    2    B     9     9    c\n",
      "    3  NaN     8     4    D\n",
      "    4    D     7     2    e\n",
      "    5    C     4     3    F\n",
      "    \n",
      "    Sort by col1\n",
      "    \n",
      "    >>> df.sort_values(by=['col1'])\n",
      "      col1  col2  col3 col4\n",
      "    0    A     2     0    a\n",
      "    1    A     1     1    B\n",
      "    2    B     9     9    c\n",
      "    5    C     4     3    F\n",
      "    4    D     7     2    e\n",
      "    3  NaN     8     4    D\n",
      "    \n",
      "    Sort by multiple columns\n",
      "    \n",
      "    >>> df.sort_values(by=['col1', 'col2'])\n",
      "      col1  col2  col3 col4\n",
      "    1    A     1     1    B\n",
      "    0    A     2     0    a\n",
      "    2    B     9     9    c\n",
      "    5    C     4     3    F\n",
      "    4    D     7     2    e\n",
      "    3  NaN     8     4    D\n",
      "    \n",
      "    Sort Descending\n",
      "    \n",
      "    >>> df.sort_values(by='col1', ascending=False)\n",
      "      col1  col2  col3 col4\n",
      "    4    D     7     2    e\n",
      "    5    C     4     3    F\n",
      "    2    B     9     9    c\n",
      "    0    A     2     0    a\n",
      "    1    A     1     1    B\n",
      "    3  NaN     8     4    D\n",
      "    \n",
      "    Putting NAs first\n",
      "    \n",
      "    >>> df.sort_values(by='col1', ascending=False, na_position='first')\n",
      "      col1  col2  col3 col4\n",
      "    3  NaN     8     4    D\n",
      "    4    D     7     2    e\n",
      "    5    C     4     3    F\n",
      "    2    B     9     9    c\n",
      "    0    A     2     0    a\n",
      "    1    A     1     1    B\n",
      "    \n",
      "    Sorting with a key function\n",
      "    \n",
      "    >>> df.sort_values(by='col4', key=lambda col: col.str.lower())\n",
      "       col1  col2  col3 col4\n",
      "    0    A     2     0    a\n",
      "    1    A     1     1    B\n",
      "    2    B     9     9    c\n",
      "    3  NaN     8     4    D\n",
      "    4    D     7     2    e\n",
      "    5    C     4     3    F\n",
      "    \n",
      "    Natural sort with the key argument,\n",
      "    using the `natsort <https://github.com/SethMMorton/natsort>` package.\n",
      "    \n",
      "    >>> df = pd.DataFrame({\n",
      "    ...    \"time\": ['0hr', '128hr', '72hr', '48hr', '96hr'],\n",
      "    ...    \"value\": [10, 20, 30, 40, 50]\n",
      "    ... })\n",
      "    >>> df\n",
      "        time  value\n",
      "    0    0hr     10\n",
      "    1  128hr     20\n",
      "    2   72hr     30\n",
      "    3   48hr     40\n",
      "    4   96hr     50\n",
      "    >>> from natsort import index_natsorted\n",
      "    >>> df.sort_values(\n",
      "    ...    by=\"time\",\n",
      "    ...    key=lambda x: np.argsort(index_natsorted(df[\"time\"]))\n",
      "    ... )\n",
      "        time  value\n",
      "    0    0hr     10\n",
      "    3   48hr     40\n",
      "    2   72hr     30\n",
      "    4   96hr     50\n",
      "    1  128hr     20\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 使用help()函数查看sort_values方法的详细帮助信息\n",
    "# help()函数可以显示任何Python对象、函数或方法的文档字符串\n",
    "# 这对于初学者了解函数参数和用法非常有用\n",
    "help(data.sort_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a579e646",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Day</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1990-12-19</td>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1990-12-20</td>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1990-12-21</td>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1990-12-24</td>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1990-12-25</td>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8468</th>\n",
       "      <td>2025-08-25</td>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8469</th>\n",
       "      <td>2025-08-26</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8470</th>\n",
       "      <td>2025-08-27</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8471</th>\n",
       "      <td>2025-08-28</td>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8472</th>\n",
       "      <td>2025-08-29</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Day  Preclose      Open   Highest    Lowest     Close\n",
       "0    1990-12-19              96.050    99.980    95.790    99.980\n",
       "1    1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "2    1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "3    1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "4    1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...         ...       ...       ...       ...       ...       ...\n",
       "8468 2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "8469 2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "8470 2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "8471 2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "8472 2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 6 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照'Day'列对数据进行升序排序（即日期从旧到新）\n",
    "# by=['Day'] 指定按'Day'列排序\n",
    "# ascending=True 表示升序排列（False表示降序）\n",
    "data = data.sort_values(by=['Day'],ascending=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b8c9cd41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1990-12-19              96.050    99.980    95.790    99.980\n",
       "1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 5 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将'Day'列设置为数据框的索引\n",
    "# set_index() 函数用于将指定列设置为DataFrame的索引\n",
    "# inplace=True 表示直接在原数据框上进行修改，不创建新的数据框\n",
    "# 设置索引后，可以通过日期直接访问对应的行数据\n",
    "data.set_index('Day', inplace = True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a92c1260",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-12-02</th>\n",
       "      <td>1434.18</td>\n",
       "      <td>1432.03</td>\n",
       "      <td>1432.03</td>\n",
       "      <td>1394.91</td>\n",
       "      <td>1395.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-03</th>\n",
       "      <td>1395.67</td>\n",
       "      <td>1391.72</td>\n",
       "      <td>1409.89</td>\n",
       "      <td>1388.51</td>\n",
       "      <td>1408.84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-04</th>\n",
       "      <td>1408.84</td>\n",
       "      <td>1414.89</td>\n",
       "      <td>1432.90</td>\n",
       "      <td>1406.61</td>\n",
       "      <td>1414.45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-05</th>\n",
       "      <td>1414.45</td>\n",
       "      <td>1411.89</td>\n",
       "      <td>1411.89</td>\n",
       "      <td>1395.08</td>\n",
       "      <td>1404.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-06</th>\n",
       "      <td>1404.88</td>\n",
       "      <td>1404.92</td>\n",
       "      <td>1417.00</td>\n",
       "      <td>1396.20</td>\n",
       "      <td>1405.53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-01-25</th>\n",
       "      <td>3128.59</td>\n",
       "      <td>3103.32</td>\n",
       "      <td>3137.00</td>\n",
       "      <td>3092.93</td>\n",
       "      <td>3094.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-01-26</th>\n",
       "      <td>3094.41</td>\n",
       "      <td>3094.85</td>\n",
       "      <td>3107.20</td>\n",
       "      <td>3001.96</td>\n",
       "      <td>3019.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-01-27</th>\n",
       "      <td>3019.39</td>\n",
       "      <td>3020.54</td>\n",
       "      <td>3028.65</td>\n",
       "      <td>2972.63</td>\n",
       "      <td>2986.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-01-28</th>\n",
       "      <td>2986.61</td>\n",
       "      <td>2985.21</td>\n",
       "      <td>3006.04</td>\n",
       "      <td>2963.89</td>\n",
       "      <td>2994.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-01-29</th>\n",
       "      <td>2994.14</td>\n",
       "      <td>2979.74</td>\n",
       "      <td>3024.85</td>\n",
       "      <td>2968.45</td>\n",
       "      <td>2989.29</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1741 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           Preclose     Open  Highest   Lowest    Close\n",
       "Day                                                    \n",
       "2002-12-02  1434.18  1432.03  1432.03  1394.91  1395.67\n",
       "2002-12-03  1395.67  1391.72  1409.89  1388.51  1408.84\n",
       "2002-12-04  1408.84  1414.89  1432.90  1406.61  1414.45\n",
       "2002-12-05  1414.45  1411.89  1411.89  1395.08  1404.88\n",
       "2002-12-06  1404.88  1404.92  1417.00  1396.20  1405.53\n",
       "...             ...      ...      ...      ...      ...\n",
       "2010-01-25  3128.59  3103.32  3137.00  3092.93  3094.41\n",
       "2010-01-26  3094.41  3094.85  3107.20  3001.96  3019.39\n",
       "2010-01-27  3019.39  3020.54  3028.65  2972.63  2986.61\n",
       "2010-01-28  2986.61  2985.21  3006.04  2963.89  2994.14\n",
       "2010-01-29  2994.14  2979.74  3024.85  2968.45  2989.29\n",
       "\n",
       "[1741 rows x 5 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用时间范围筛选数据，获取2002年12月到2010年1月的数据\n",
    "# 当'Day'列被设置为索引后，可以直接使用日期范围进行筛选\n",
    "# '2002-12':'2012-01'表示从2002年12月开始到2010年1月结束的时间范围\n",
    "# 这种筛选方式对于时间序列数据分析非常有用\n",
    "data['2002-12':'2010-01']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73c286e5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15b3e771",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78473d96",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71575358",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc0e3431",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
